//
//  setPSVC.m
//  tcpoctest
//
//  Created by gump on 11/7/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "setPSVC.h"

@interface setPSVC ()

@end

@implementation setPSVC

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"修改密码";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    //[backItem release];
    
    UIBarButtonItem *modifyItem = [self createBarButtonItemWithTitle:@"提交" target:self action:@selector(modifyButtonPressed)];
    self.navItem.rightBarButtonItem = modifyItem;
    //[modifyItem release];
    
    UIImageView *inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 55, 300, 44)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [self.view addSubview:inputBgView];
    //[inputBgView release];
    
    inputTF = [[UITextField alloc]initWithFrame:CGRectMake(5, 0, 290, 44)];
    inputTF.font = CENTER_CELL_CONTENT_FONT;
    inputTF.textColor=[UIColor colorWithWhite:0.4 alpha:1.0];
    inputTF.borderStyle=UITextBorderStyleNone;
    inputTF.leftViewMode = UITextFieldViewModeAlways;
    inputTF.placeholder = @"请输入旧密码";
    inputTF.secureTextEntry = YES;
    inputTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    inputTF.delegate =self;
    [inputBgView addSubview:inputTF];
    //[inputTF release];
    
    inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 55 + 44, 300, 44)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [self.view addSubview:inputBgView];
    //[inputBgView release];
    
    newPSTF = [[UITextField alloc]initWithFrame:CGRectMake(5, 0, 290, 44)];
    newPSTF.font=CENTER_CELL_CONTENT_FONT;
    newPSTF.textColor=[UIColor colorWithWhite:0.4 alpha:1.0];
    newPSTF.borderStyle=UITextBorderStyleNone;
    newPSTF.leftViewMode = UITextFieldViewModeAlways;
    newPSTF.delegate =self;
    newPSTF.placeholder = @"请输入新密码";
    newPSTF.secureTextEntry = YES;
    newPSTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [inputBgView addSubview:newPSTF];
    //[newPSTF release];
    
    inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 55 + 44 + 44, 300, 44)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [self.view addSubview:inputBgView];
    //[inputBgView release];
    
    repeatTF = [[UITextField alloc]initWithFrame:CGRectMake(5, 0, 290, 44)];
    repeatTF.font=CENTER_CELL_CONTENT_FONT;
    repeatTF.textColor=[UIColor colorWithWhite:0.4 alpha:1.0];
    repeatTF.borderStyle=UITextBorderStyleNone;
    repeatTF.leftViewMode = UITextFieldViewModeAlways;
    repeatTF.delegate =self;
    repeatTF.placeholder = @"请重复输入新密码";
    repeatTF.secureTextEntry = YES;
    repeatTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [inputBgView addSubview:repeatTF];
    //[repeatTF release];
}

#pragma mark -private fun-
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)modifyButtonPressed
{
    do {
        if([inputTF.text length] == 0 || [newPSTF.text length] == 0)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"资料不完整"
                                                            message:@"请将资料填写完整"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            //[alert release];
            break;
        }
        
        if (![newPSTF.text isEqualToString:repeatTF.text])
        {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"新旧密码一致"
                                                            message:@"请重新填写新密码"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            //[alert release];
            break;
        }
        
        [DataControl shareControl].strChangeOldPS = md5(inputTF.text);
        [DataControl shareControl].strChangeNewPS = md5(newPSTF.text);
        
        [[DataControl shareControl].LCDS addLogicCommand:LC_SETPS];
        
        [[DataControl shareControl] SetUpdateUserType:UUT_PS];
        
        [[ViewManager sharedViewManager] backView:self];
    } while (false);
    
}

-(void)viewWillShow
{
    inputTF.text = @"";
    newPSTF.text = @"";
    repeatTF.text = @"";
}

#pragma mark -textfield delegate-
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return true;
}

@end
